<template>
    <div class="login">
        <s-header :name="type == 'login' ? '登录' : '注册'" :back="'/home'"></s-header>
<!--        <img class="logo" src="//s.weituibao.com/1582958061265/mlogo.png" alt="">-->
        <svg-icon icon-class="logo" class="logo" ></svg-icon>
        <br>
        <div v-if="type == 'login'" class="login-body login">
            <van-form @submit="onSubmit">
                <van-field
                        v-model="username"
                        name="username"
                        label="用户名"
                        placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]"
                />
                <van-field
                        v-model="password"
                        type="password"
                        name="password"
                        label="密码"
                        placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]"
                />
                <div style="margin: 16px;">
                    <div class="link-register" @click="toggle('register')">立即注册</div>
                    <van-button round block type="info" color="#1baeae" native-type="submit">登录</van-button>
                </div>
            </van-form>
        </div>
        <div v-else class="login-body register">
            <van-form @submit="onSubmit">
                <van-field
                        v-model="username1"
                        name="username1"
                        label="用户名"
                        placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }]"
                />
                <van-field
                        v-model="password1"
                        type="password"
                        name="password1"
                        label="密码"
                        placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]"
                />
                <div style="margin: 16px;">
                    <div class="link-login" @click="toggle('login')">已有登录账号</div>
                    <van-button round block type="info" color="#1baeae" native-type="submit">注册</van-button>
                </div>
            </van-form>
        </div>
    </div>
</template>

<script>
    import sHeader from '../components/SimpleHeader'
    // import { login, register, getUserInfo } from '../service/user'
    import {setLocal, getLocal} from '../common/js/utils'

    import {Toast} from 'vant'

    export default {
        name: "index",
        data() {
            return {
                username: '',
                password: '',
                username1: '',
                password1: '',
                type: 'login',
            }
        },
        components: {
            sHeader,
        },
        methods: {
            toggle(v) {
                this.type = v
            },
            async onSubmit(values) {
                if (this.type == 'login') {
                    const data = {
                        "username": values.username,
                        "password": values.password
                    }
                    this.$store.dispatch('Login', data).then(() =>{
                        Toast.success('登录成功');
                        setTimeout(() => {
                            window.location.href = '/'
                        }, 3000)
                    })
                } else {
                    const {data} = await register({
                        "loginName": values.username1,
                        "password": values.password1
                    })
                    Toast.success('注册成功')
                    this.type = 'login'
                }
            },
            success(obj) {
                // 回调之后，刷新验证码
                obj.refresh()
            },
            error(obj) {
                // 回调之后，刷新验证码
                obj.refresh()
            }
        }
    }
</script>


<style lang="less">
    .login {
        .logo {
            width: 120px;
            height: 120px;
            display: block;
            margin: 80px auto 0px;
        }

        .login-body {
            padding: 0 20px;
        }

        .login {
            .link-register {
                font-size: 14px;
                margin-bottom: 20px;
                color: #1989fa;
                display: inline-block;
            }
        }

        .register {
            .link-login {
                font-size: 14px;
                margin-bottom: 20px;
                color: #1989fa;
                display: inline-block;
            }
        }

    }
</style>
